import { defineStore } from 'pinia'

export const useTutorStore = defineStore('tutor', {
  state: () => ({
    // 导师列表
    tutors: [
      {
        id: '1',
        name: '张伟',
        degree: '博士',
        school: '清华大学',
        major: '环境会计',
        desc: '我是来自清华大学的张伟，在读博士，已发表2篇SCI。已发表2篇SCI已发表2篇SCI已发表2篇SCI已发表2篇SCI已发表2……',
        avatar: '/static/logo.png',
        verified: true,
        orders: 12,
        canHelp: ['论文辅导', '专业课辅导', '保研辅导'],
        category: '博士',
        rating: 4.8,
        responseTime: '1小时内',
        price: '200-500/小时',
        experience: '3年教学经验',
        tags: ['专业负责', '耐心细致', '响应及时'],
        reviews: [
          {
            id: '1',
            userName: '学生A',
            userAvatar: '/static/logo.png',
            type: '论文辅导',
            score: 5,
            date: '2025-01-10',
            content: '张老师非常专业，论文指导很详细，帮助很大！'
          },
          {
            id: '2',
            userName: '学生B',
            userAvatar: '/static/logo.png',
            type: '保研辅导',
            score: 5,
            date: '2025-01-08',
            content: '保研材料准备得很充分，面试指导也很到位。'
          }
        ]
      },
      {
        id: '2',
        name: '李明',
        degree: '硕士',
        school: '北京大学',
        major: '金融',
        desc: '擅长金融、会计等领域辅导，耐心细致，有丰富的教学经验。',
        avatar: '/static/logo.png',
        verified: false,
        orders: 8,
        canHelp: ['作业辅导', '考研辅导'],
        category: '硕士',
        rating: 4.5,
        responseTime: '2小时内',
        price: '150-300/小时',
        experience: '2年教学经验',
        tags: ['态度友好', '讲解清晰'],
        reviews: [
          {
            id: '3',
            userName: '学生C',
            userAvatar: '/static/logo.png',
            type: '作业辅导',
            score: 4,
            date: '2025-01-05',
            content: '老师很耐心，作业讲解得很清楚。'
          }
        ]
      },
      {
        id: '3',
        name: '王教授',
        degree: '教授',
        school: '复旦大学',
        major: '计算机科学',
        desc: '复旦大学计算机系教授，专业从事人工智能研究20年。',
        avatar: '/static/logo.png',
        verified: true,
        orders: 25,
        canHelp: ['论文辅导', '项目指导', '学术规划'],
        category: '高校讲师',
        rating: 4.9,
        responseTime: '30分钟内',
        price: '500-1000/小时',
        experience: '20年教学经验',
        tags: ['经验丰富', '学术权威', '指导专业'],
        reviews: [
          {
            id: '4',
            userName: '学生D',
            userAvatar: '/static/logo.png',
            type: '论文指导',
            score: 5,
            date: '2025-01-12',
            content: '王教授的指导非常专业，论文质量提升很大。'
          }
        ]
      }
    ],
    
    // 当前选中的导师
    currentTutor: null,
    
    // 筛选条件
    filterOptions: {
      category: '', // 推荐、高校讲师、博士、硕士、本科
      verified: null, // true/false/null
      priceRange: '', // 价格区间
      rating: 0 // 最低评分
    }
  }),
  
  getters: {
    // 根据分类筛选导师
    getTutorsByCategory: (state) => (category) => {
      if (!category || category === '推荐') {
        return state.tutors
      }
      return state.tutors.filter(tutor => tutor.category === category)
    },
    
    // 根据ID获取导师
    getTutorById: (state) => (id) => {
      return state.tutors.find(tutor => tutor.id === id)
    },
    
    // 获取推荐导师（按评分和订单数排序）
    getRecommendedTutors: (state) => {
      return [...state.tutors].sort((a, b) => {
        // 先按评分排序，再按订单数排序
        if (b.rating !== a.rating) {
          return b.rating - a.rating
        }
        return b.orders - a.orders
      })
    },
    
    // 获取已认证导师
    getVerifiedTutors: (state) => {
      return state.tutors.filter(tutor => tutor.verified)
    },
    
    // 根据筛选条件获取导师
    getFilteredTutors: (state) => {
      let filtered = [...state.tutors]
      
      // 按分类筛选
      if (state.filterOptions.category && state.filterOptions.category !== '推荐') {
        filtered = filtered.filter(tutor => tutor.category === state.filterOptions.category)
      }
      
      // 按认证状态筛选
      if (state.filterOptions.verified !== null) {
        filtered = filtered.filter(tutor => tutor.verified === state.filterOptions.verified)
      }
      
      // 按评分筛选
      if (state.filterOptions.rating > 0) {
        filtered = filtered.filter(tutor => tutor.rating >= state.filterOptions.rating)
      }
      
      return filtered
    }
  },
  
  actions: {
    // 设置当前导师
    setCurrentTutor(tutor) {
      this.currentTutor = tutor
    },
    
    // 更新筛选条件
    updateFilter(filterOptions) {
      this.filterOptions = { ...this.filterOptions, ...filterOptions }
    },
    
    // 清空筛选条件
    clearFilter() {
      this.filterOptions = {
        category: '',
        verified: null,
        priceRange: '',
        rating: 0
      }
    },
    
    // 添加导师评价
    addTutorReview(tutorId, review) {
      const tutor = this.tutors.find(t => t.id === tutorId)
      if (tutor) {
        tutor.reviews.unshift({
          id: Date.now().toString(),
          date: new Date().toLocaleDateString(),
          ...review
        })
        
        // 重新计算平均评分
        const totalScore = tutor.reviews.reduce((sum, r) => sum + r.score, 0)
        tutor.rating = (totalScore / tutor.reviews.length).toFixed(1)
      }
    },
    
    // 联系导师
    contactTutor(tutorId) {
      const tutor = this.tutors.find(t => t.id === tutorId)
      if (tutor) {
        // 这里可以实现联系导师的逻辑
        console.log('联系导师:', tutor.name)
        return tutor
      }
    },
    
    // 收藏导师
    favoriteTutor(tutorId) {
      const tutor = this.tutors.find(t => t.id === tutorId)
      if (tutor) {
        tutor.isFavorite = !tutor.isFavorite
      }
    },
    
    // 搜索导师
    searchTutors(keyword) {
      if (!keyword) return this.tutors
      
      const lowerKeyword = keyword.toLowerCase()
      return this.tutors.filter(tutor => 
        tutor.name.toLowerCase().includes(lowerKeyword) ||
        tutor.school.toLowerCase().includes(lowerKeyword) ||
        tutor.major.toLowerCase().includes(lowerKeyword) ||
        tutor.desc.toLowerCase().includes(lowerKeyword) ||
        tutor.canHelp.some(help => help.toLowerCase().includes(lowerKeyword))
      )
    }
  }
})
